﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Collections;
using System.ComponentModel;
using Dycox.Linq;

namespace Dycox.Windows.Forms.Design
{
    internal class QueryFieldValueUIService : IPropertyValueUIService
    {
        PropertyValueUIHandler _valueUIHandler;

        public void AddPropertyValueUIHandler(PropertyValueUIHandler newHandler)
        {
            _valueUIHandler += newHandler;
        }

        public PropertyValueUIItem[] GetPropertyUIValueItems(System.ComponentModel.ITypeDescriptorContext context, System.ComponentModel.PropertyDescriptor propDesc)
        {
            ArrayList list = new ArrayList();

            if (propDesc is QueryProperty)
            {
                bool active = !((QueryProperty)propDesc).Suspended;
                PropertyValueUIItem item = new PropertyValueUIItem(
                    active ? Properties.Resources.InputFieldActive8 : Properties.Resources.InputFieldSuspended8,
                    this.PropUiHandler, null);
                list.Add(item);
            }

            if (_valueUIHandler != null)
                _valueUIHandler(context, propDesc, list);

            return (PropertyValueUIItem[])list.ToArray(typeof(PropertyValueUIItem));
        }

        private void PropUiHandler(ITypeDescriptorContext context, PropertyDescriptor propDesc, PropertyValueUIItem invokedItem)
        {
            QueryProperty qfpd = propDesc as QueryProperty;
            if (qfpd != null) qfpd.Suspended = !qfpd.Suspended;
            
        }

        public void NotifyPropertyValueUIItemsChanged()
        {
            if (this.PropertyUIValueItemsChanged != null)
                this.PropertyUIValueItemsChanged(this, EventArgs.Empty);
        }

        public event EventHandler PropertyUIValueItemsChanged;

        public void RemovePropertyValueUIHandler(PropertyValueUIHandler newHandler)
        {
            _valueUIHandler -= newHandler;
        }
    }
}
